const mutations = {
  //更新仓库中收藏列表的数据
  updateGoodsSave(state, goods) {
    let res = state.goods_save.find(x => x.goods_id === goods.goods_id)
    //如果res为false，说明已经没有收藏过了
    if (!res) {
      state.goods_save.push(goods)
      this.commit('goods_save/SAVETOSTORAGE')
      return uni.$showMsg('收藏成功!')
    } else return uni.$showMsg('您已经收藏过此商品!')
  },
  //删除收藏列表的某个商品
  deleteGoods(state, goods) {
    //过滤出不是选中商品
    let newGoodSave = state.goods_save.filter(x => x.goods_id !== goods.goods_id)
    state.goods_save = newGoodSave
    this.commit('goods_save/SAVETOSTORAGE')
  },

  //持久化存储 —— 本地存储
  SAVETOSTORAGE(state, value) {
    uni.setStorageSync('save_list', JSON.stringify(state.goods_save))
  }

}

const state = {
  goods_save: JSON.parse(uni.getStorageSync('save_list') || '[]'),
}

const getters = {
  //收藏物品的个数
  save_length(state) {
    return state.goods_save.length
  }
}

export default {
  namespaced: true,
  mutations,
  getters,
  state
}
